﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Drawing;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Anaglyph
{

    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {    
        public MainWindow()
        {
            InitializeComponent();
        }

        private void To_Change_IMG(Image img) { 
            //загружаем рисунок
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".jpg";
            dlg.Filter = "Images|*.jpg;*.png;*.bmp";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                string filename = dlg.FileName;
                BitmapImage tBitmap = new BitmapImage(new Uri(filename, UriKind.Absolute));
                img.Source = tBitmap;
            }
            //проверяем что загрузилось
            if (ImageRight.Source == null || ImageLeft.Source == null)
            {
                Label_hit.Content = "";
                TabItem_Result.Visibility = Visibility.Hidden;
                return;
            }
            if ((((BitmapSource)ImageRight.Source).Height == ((BitmapSource)ImageLeft.Source).Height) && 
                (((BitmapSource)ImageRight.Source).Width == ((BitmapSource)ImageLeft.Source).Width))
            {
                Label_hit.Content = "";
                TabItem_Result.Visibility = Visibility.Visible;
            }
            else
            { 
                Label_hit.Content = "Size don't valid";
                TabItem_Result.Visibility = Visibility.Hidden;
            }
        }

        private void Button_Click_R(object sender, RoutedEventArgs e)
        {
            To_Change_IMG(ImageRight);
        }

        private void Button_Click_L(object sender, RoutedEventArgs e)
        {
            To_Change_IMG(ImageLeft);
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //если выбран нужный таб то конвертируем
            if (((TabControl)sender).SelectedIndex == 1) {
                BitmapSource biR = (BitmapSource)ImageRight.Source;
                BitmapSource biL = (BitmapSource)ImageLeft.Source;
                if(biR.Format!=PixelFormats.Bgr24)
                    biR = new FormatConvertedBitmap(biR, PixelFormats.Bgr24, null, 0);
                if(biL.Format!=PixelFormats.Bgr24)
                    biL = new FormatConvertedBitmap(biL, PixelFormats.Bgr24, null, 0);
                int width = biR.PixelWidth;
                int height = biL.PixelHeight;
                Byte []tmp1=new Byte[3*width*height];
                Byte []tmp2=new Byte[3*width*height];
                biR.CopyPixels(tmp1,3*width,0);
                biL.CopyPixels(tmp2,3*width,0);
                for (long i = 0; i < tmp1.LongLength; i += 3) {
                    tmp1[i + 2] = tmp2[i + 2];
                }
                ImageResult.Source = BitmapImage.Create(width, height, 96, 96, PixelFormats.Bgr24, null, tmp1, 3 * width);
            }
        }

        private void MenuItem_Click_LeftRight(object sender, RoutedEventArgs e)
        {
            WindowFSLeftRight wind=new WindowFSLeftRight();
            wind.Owner = this;
            wind.setImage(ImageLeft.Source, ImageRight.Source);
            wind.ShowDialog();
        }

        private void MenuItem_Click_TopBottom(object sender, RoutedEventArgs e)
        {
            WindowFSTopBottom wind = new WindowFSTopBottom();
            wind.Owner = this;
            wind.setImage(ImageLeft.Source, ImageRight.Source);
            wind.ShowDialog();
        }
    }
}
